﻿using System;
using System.Linq;
using System.Web.Mvc;
using CaptchaMvc.Infrastructure;
using Doan.Website.Models;
using CaptchaMvc.HtmlHelpers;
using PagedList;

namespace Doan.Website.Controllers
{
    public class ShareController : Controller
    {
        // Lấy dữ liệu hiển thị footer,header 
        private static DoAnWebsiteEntities db = new DoAnWebsiteEntities();
        #region Comment

        public ActionResult Index(int id, int? page)
        {
            ViewBag.Id = id;
            Session["NewsIdComment"] = id;
            const int pageSize = 5;
            var temp = ((db.Comments.Count(x => x.NewsId == id)) / pageSize) + 1;
            if (((db.Comments.Count(x => x.NewsId == id)) % pageSize) == 0) temp -= 1;
            int pageNumber;
            if(temp==0) pageNumber = (page ?? 1);
            //ViewBag.lastPage = pageNumber;
            else
            {
                pageNumber = (page ?? temp);
            }
            var comment = db.Comments.Where(x => x.NewsId == id).OrderBy(m => m.CommentId).OrderBy(x => x.Date).ToPagedList(pageNumber, pageSize);
            ViewBag.pageNumber = pageNumber;
            ViewBag.OnePageOfComment = comment;
            return PartialView("Index");
        }

        [HttpPost]
        public ActionResult CreateComment([Bind(Exclude = "Date")] Comment comment)
        {
            int temp = (int)Session["NewsIdComment"];
            if (this.IsCaptchaVerify("Captcha is not valid"))
            {
                ViewBag.ErrorCaptcha = "";
                TempData["Message"] = "Message: captcha is valid.";
                comment.Date = DateTime.Now;
                comment.Active = true;
                comment.NewsId = temp;
                db.AddToComments(comment);
                db.SaveChanges();
                return RedirectToAction("Index", "Share", new { id = temp });
            }
            else
            {
                TempData["ErrorMessage"] = "Error: captcha is not valid.";
                ViewBag.ErrorCaptcha = "Nhập không đúng mã";
                return RedirectToAction("Index", "Share", new { id = temp });
            }
        }
        public ActionResult ChangeContainerProvider()
        {
            if (CaptchaUtils.CaptchaManager.StorageProvider is CookieStorageProvider)
                CaptchaUtils.CaptchaManager.StorageProvider = new SessionStorageProvider();
            else
                CaptchaUtils.CaptchaManager.StorageProvider = new CookieStorageProvider();
            return RedirectToAction("Index", "Home");
        }

        // Delete Comment  
        [AccessDeniedAuthorize(Roles = "administrators, users")]
        public ContentResult DeleteComment(int id)
        {
            var comment = db.Comments.Single(x=>x.CommentId==id);
            db.Comments.DeleteObject(comment);
            db.SaveChanges();
            return Content("Bài đã xóa");
        }

        #endregion

        #region bài viết đặc biệt thuộc trang chủ và trang thành viên
        // Hiển thị 10 bài hot nhất
        public ActionResult TopNews()
        {
            var bv = db.News.Where(x => x.Active).OrderByDescending(x => x.Views).Take(10);
            return PartialView("TopNews",bv);
        }

        public ActionResult ListCancerPartial()
        {
            var cancer = db.Cancers.Where(x => x.Active).OrderBy(x => x.CancerName);
            return PartialView("ListCancerPartial", cancer);
        }

        // 2 bài viết có cùng menu tiep theo
        public ActionResult HaiBaiTiep(int id)
        {
            var bv = db.News.Where(x => x.GroupNewsId == id && x.Active).OrderByDescending(m => m.NewsId).Skip(1).Take(2);
            return PartialView("HaiBaiTiep", bv);
        }

        // 3 bài viết có cùng menu tiep theo 
        public ActionResult BaiVietLienQuan(int id, int? noBv)
        {
            var bv = db.News.Where(x => x.GroupNewsId == id && x.Active).OrderByDescending(m => m.NewsId).Skip(3).Take(3);
            return PartialView("BaiVietLienQuanPartial", bv);
        }

        // 5 bài viết có cùng menu 
        public ActionResult NamBaiLienQuan(int id, int bvId)
        {
            var bv = db.News.Where(x => x.GroupNewsId == id && x.NewsId != bvId && x.Active).OrderByDescending(m => m.NewsId).Take(5);
            return PartialView("NamBaiLienQuan", bv);
        }

        //Slide
        public ActionResult SlideShowPartial()
        {
            var bv = db.News.Where(x=>x.Home);
            return PartialView("SlideShowPartial", bv);
        }

        //submenubox
        public ActionResult Submenubox(int id)
        {
            var bv = db.GroupNews.SingleOrDefault(x => x.GroupNewsId == id && x.Active);
            return PartialView("Submenubox", bv);
        }

        public ActionResult Menu()
        {
            var cate = db.GroupNews.Where(x=>x.Active).OrderBy(x=>x.GroupNewsId);
            return PartialView("Menu", cate);
        }

        //tagClound

        public ActionResult TagCloundPartial()
        {
            var tag = db.Tags.ToList().OrderByDescending(x=>x.News.Count).Take(10);
            return PartialView("TagCloundPartial", tag);
        }

        public ActionResult TagBarPartial(int id)
        {
            var news = db.News.SingleOrDefault(x => x.NewsId == id);
            var tag = news.Tags;
            return PartialView("TagBarPartial", tag);
        }

        [HttpGet]
        public ActionResult CreateQuestion()
        {
            var query = db.GroupNews.Select(c => new { c.GroupNewsId, c.GNName });
            ViewBag.Group = new SelectList(query.AsEnumerable(), "GroupNewsId", "GNName");
            return PartialView("CreateQuestion");
        }

        [HttpPost]
        public ActionResult CreateQuestion(Ask model)
        {
            var query = db.GroupNews.Select(c => new { c.GroupNewsId, c.GNName });
            ViewBag.Group = new SelectList(query.AsEnumerable(), "GroupNewsId", "GNName");
            model.CreateDate = DateTime.Now;
            model.Active = true;
            db.AddToAsks(model);
            db.SaveChanges();

            var answer = new Answer();
            answer.AskId = model.AskId;
            answer.Content = "<b>" + model.Content + "</b" + "\n Chưa có câu trả lời cho câu hỏi này...";
            answer.Active = true;
            answer.CreateDate = DateTime.Now;
            db.AddToAnswers(answer);
            db.SaveChanges();

            ViewBag.QSuccess = "Câu hỏi đã được gửi thành công. Chúng tôi sẽ trả lời bạn trong thời gian sớm nhất";
            return PartialView("CreateQuestion");
        }

        #endregion
    }
}
